package com.polaris.live.common.constant

import com.polaris.live.common.network.readEncryptKey1
import com.polaris.live.common.network.readEncryptKey2

/**
 * ServerConfig
 *
 * @author Created by lucas on 2023/10/24 13:38
 */
object ServerConfig {

    private var debug: Boolean = false
    private var configType: ConfigType = ConfigType.PROD
    private var serverMode: ServerMode? = null

    /**
     * 通过字符串初始化
     */
    fun initConfig(configType: String, debug: Boolean) {
        when (configType) {
            ConfigType.TEST.value -> updateInitConfig(ConfigType.TEST)
            ConfigType.PRE.value -> updateInitConfig(ConfigType.PRE)
            else -> updateInitConfig(ConfigType.PROD)
        }
        ServerConfig.debug = debug
    }

    /**
     * 通过自定义的初始化
     */
    fun initCustomConfig(
        configType: String,
        debug: Boolean,
        url: String? = null,
        domains: Set<String>? = null
    ) {
        initConfig(configType, debug)

        invokeUpdateConfig(url, domains)
    }

    /**
     * 通过枚举更新
     */
    fun updateInitConfig(configType: ConfigType) {
        ServerConfig.configType = configType
        serverMode = when (configType) {
            ConfigType.TEST -> {
                TestServerMode
            }
            ConfigType.PRE -> {
                PreServerMode
            }
            ConfigType.PROD -> {
                ProdServerMode
            }
        }
    }

    /**
     * 仅修改url
     */
    fun updateCustomConfig(url: String? = null, domains: Set<String>? = null) {
        initConfig(configType.value, debug)

        invokeUpdateConfig(url, domains)
    }

    private fun invokeUpdateConfig(url: String?, domains: Set<String>?) {
        val oldServerMode = getServerMode()
        serverMode = CustomServerMode(
            invokeGetServerMode(),
            url ?: oldServerMode.url,
            domains ?: oldServerMode.domains
        )
    }

    fun getConfigMode(): ConfigType {
        return configType
    }

    fun getServerMode(): ServerMode {
        return serverMode ?: invokeGetServerMode()
    }

    private fun invokeGetServerMode(): ServerMode {
        return when (configType) {
            ConfigType.TEST -> TestServerMode
            ConfigType.PRE -> PreServerMode
            ConfigType.PROD -> ProdServerMode
        }
    }

    fun getServerUrl(): String {
        return getServerMode().url
    }

    /**
     * 是测试服务
     */
    fun isTestServer(): Boolean {
        return configType == ConfigType.TEST
    }

    /**
     * 是预发服务
     */
    fun isPreServer(): Boolean {
        return configType == ConfigType.PRE
    }

    /**
     * 是线上服务
     */
    fun isProdServer(): Boolean {
        return configType == ConfigType.PROD
    }

    /**
     * 是否为debug
     */
    fun isDebug(): Boolean {
        return debug
    }
}

enum class ConfigType(
    val value: String
) {

    TEST("test"),
    PRE("pre"),
    PROD("prod")

}

sealed class ServerMode {

    /**
     * 请求地址
     */
    abstract val url: String

    /**
     * domain的地址
     */
    abstract val domains: Set<String>

    /**
     * 秘钥
     */
    abstract val encrypt: String

    /**
     * 安全的秘钥
     */
    abstract val secEncrypt: String

}

object TestServerMode : ServerMode() {

    override val url: String = "https://wejoy.polaristest.net"

    override val domains: Set<String> = setOf("https://domain.polaristest.net")

    override val encrypt: String = "kGJGJBTNcPI3t0NnWWe60hOcKXuxpyo7"

    override val secEncrypt: String = "De6KzabNFDDfEHFz"

}

object PreServerMode : ServerMode() {

    override val url: String = "https://wejoy.polaristest.net"

    override val domains: Set<String> = setOf("https://domain.polaristest.net")

    override val encrypt: String = "kGJGJBTNcPI3t0NnWWe60hOcKXuxpyo7"

    override val secEncrypt: String = "De6KzabNFDDfEHFz"

}

object ProdServerMode : ServerMode() {

    override val url: String = "https://wejoy.polaristech.net"

    override val domains: Set<String> = setOf("https://domain.polarisapp.net", "https://domain.partypolaris.com")

    private var _encrypt: String? = null

    //线上和预发的key不允许明文存储
    override val encrypt: String
        get() = _encrypt ?: let {
            readEncryptKey1("tJ4DF/vBOxSjG3OctdZqVkbulbDc58zf6ftX5CUEzptCvUzbQFB7yeY9/cf4RXTEj0SCgbsGeQrzHr1JNedu+NBzfhiaVj4xLYPKrZk2GxX2/9dkJpHj4KlTu0+DsDrG").apply {
                _encrypt = this
            }
        }

    private var _secEncrypt: String? = null

    override val secEncrypt: String
        get() = _secEncrypt ?: let {
            readEncryptKey2("CtDo5dl36mdju2v1nw0vc6L5sVKD9QXlI0w/XIKrLXOIxbm9K+HAUqBGXz8a5E+iqbEqbf8P1DNf7e2af3ya0OMEvS2Ys2d/XrgPPZpSWiODYtdkELghJUNyowfVhVmv").apply {
                _secEncrypt = this
            }
        }

}

class CustomServerMode(
    delegate: ServerMode,
    override val url: String,
    override val domains: Set<String>
) : ServerMode() {

    override val encrypt: String = delegate.encrypt

    override val secEncrypt: String = delegate.secEncrypt

}